// pages/user/recharge.js
import http from '../../utils/http.js'
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    choseActivityId:'',
    value:"",
    recharging:false,
    configs:[],
    width:0,
    isFirstRecharge:false
  },
  loadConfigs(){
    let me=this;
    let userId = wx.getStorageSync("userId");
    http.request({
      url: 'rechargeActivity/getList/'+userId,
      success: res => {
        me.setData({
          configs:res.configs,
          isFirstRecharge: res.isFirstRecharge
        });
      }
    })
  },
  bindRechargeVal(e){
    let fillMoney = parseFloat(e.currentTarget.dataset.re.rechargeMoney);
    let giveMoney = parseFloat(e.currentTarget.dataset.re.giveMoney);
    let firstGiveMoney = parseFloat(e.currentTarget.dataset.re.firstGiveMoney);
    let sendMoney = !this.data.isFirstRecharge ? giveMoney : firstGiveMoney;
    this.setData({
      value: fillMoney + sendMoney,
      giveMoney: sendMoney,
      fillMoney: fillMoney,
      choseActivityId: e.currentTarget.dataset.re.id
    });
  },
  recharge(){
    let me=this;
    let userId = wx.getStorageSync("userId");
    me.setData({recharging:true});
    http.request({
      url:'user/recharge',
      data:{
        userId: userId,
        value:me.data.value,
        fillMoney:me.data.fillMoney,
        giveMoney:me.data.giveMoney,
        choseActivityId: me.data.choseActivityId,
        direction:1
      },
      success:res=>{
        me.createPrepay(res);
      }
    })
  },

  createPrepay(orderCode) {
    let me = this;
    let userId = wx.getStorageSync("userId");
    http.request({
      url: 'wxPay/recharge',
      data: {
        tradeId: orderCode,
        wxKey: app.getSessionKey(),
        userId: userId
      },
      success: res => {
        if (res.code == "200") {
          if (res.msg) {
            var msg = res.msg;
            wx.requestPayment({
              timeStamp: msg.timeStamp.toString(),
              nonceStr: msg.nonceStr,
              package: msg.package,
              signType: 'MD5',
              paySign: msg.sign,
              success: res => {
                me.setData({ recharging: false });
                wx.showToast({
                  title: '充值成功'
                })
                setTimeout(()=>{
                  wx.navigateBack()
                },1500)
              },
              fail:res=>{
                if (res.errMsg === 'requestPayment:fail cancel') {
                  wx.showToast({
                    icon: 'none',
                    title: '用户取消充值'
                  })
                  me.setData({ recharging: false });
                }
              }
            })
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '余额充值',
    })
    this.loadConfigs();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})